
//
//  UIBarButtonItem+Extension.swift
//  sina
//
//  Created by 崔正强 on 17/3/20.
//  Copyright © 2017年 崔正强. All rights reserved.
//

import UIKit

extension UIBarButtonItem{
    
    //给UIBarButtonItem扩充快速创建对象的方法
    //给函数的参数设置默认值 系统会自动生成多个函数
    convenience init(title:String? = nil, backImage:String? = nil,imageName:String? = nil, target: Any?, action: Selector?) {
        //分类中没有super
        let btn = UIButton()
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.setTitle(title, for: .normal)
        if let img = imageName {
            btn.setImage(UIImage(named:img), for: .normal)
            btn.setImage(UIImage(named:img + "_highlighted"), for: .highlighted)
        }
        
        if let t = target, let a = action {
            btn.addTarget(t, action: a, for: .touchUpInside)
        }
        btn.sizeToFit()
        
        //使用self调用指定的构造函数创建对象
        self.init()
        
        self.customView = btn
    }
    
}
